package de.koller.million.render.obj.base;

import de.koller.million.render.RenderRoot;
import de.koller.million.render.obj.IRenderGL;
import de.koller.oceanFramework.gl.util.TextureUtil;
import org.lwjgl.opengl.GL11;

public class Cube implements IRenderGL {

	private final RenderRoot rroot;
	private int[] texture;
	private float w, d, h;
	private Rect3D rect;

	public Cube( RenderRoot rroot, float w, float d, float h ) {
		this.w = w;
		this.d = d;
		this.h = h;
		
		this.rroot = rroot;
		rect = new Rect3D( rroot, 1, 1 );
	}
	
	public void setTexture( int front, int left, int back, int right, int top, int bottom ) {
		texture = new int[] { front, left, back, right, top, bottom };
	}
	
	@Override
	public void renderThis() {
		frontRender();
		leftRender();
		backRender();
		rightRender();
		topRender();
	}
	
	private void frontRender() {
		GL11.glPushMatrix();
		GL11.glTranslatef( 0, 0, d/2 );
		if( texture != null ) TextureUtil.bind( texture[0] );
		rect.set( w, h );
		rect.renderThis();
		GL11.glPopMatrix();
	}
	
	private void leftRender() {
		GL11.glPushMatrix();
		GL11.glTranslatef( -w/2, 0, 0 );
		GL11.glRotatef( 270, 0, 1, 0 );
		if( texture != null ) TextureUtil.bind( texture[1] );
		rect.set( d, h );
		rect.renderThis();
		GL11.glPopMatrix();
	}

	private void backRender() {
		GL11.glPushMatrix();
		GL11.glTranslatef( 0, 0, -d/2 );
		GL11.glRotatef( 180, 0, 1, 0 );
		if( texture != null ) TextureUtil.bind( texture[2] );
		rect.set( w, h );
		rect.renderThis();
		GL11.glPopMatrix();
	}

	private void rightRender() {
		GL11.glPushMatrix();
		GL11.glTranslatef( w/2, 0, 0 );
		GL11.glRotatef( 90, 0, 1, 0 );
		if( texture != null ) TextureUtil.bind( texture[3] );
		rect.set( d, h );
		rect.renderThis();
		GL11.glPopMatrix();
	}

	private void topRender() {
		GL11.glPushMatrix();
		GL11.glTranslatef( 0, h/2, 0 );
		GL11.glRotatef( 270, 1, 0, 0 );
		if( texture != null ) TextureUtil.bind( texture[4] );
		rect.set( w, d );
		rect.renderThis();
		GL11.glPopMatrix();
	}
	
}
